package com.ms.action;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/ServicesAction/")
public class ServicesAction {

	@RequestMapping("loadModule.action")
	public void loadModule(HttpServletRequest request, HttpServletResponse response, Module module) {
		if(module.getType().equals("frame")) {
			returnFrameModul(request, response, module);
		}
		else {
			returnModul(request, response, module.getModuleId());
		}
	}
	
	private void returnFrameModul(HttpServletRequest request, HttpServletResponse response, Module module) {
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			PrintWriter out = response.getWriter();
			InputStream inStream = ServicesAction.class.getResource("framemodule.tlp").openStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
			String line = null;
			while((line = reader.readLine()) != null) {
				if(line.contains("$urlpath$"))
					line = line.replace("$urlpath$", module.getUrlpath());
				
				if(line.contains("$className$")) {
					line = line.replace("$className$", module.getClassName());
				}
				
				if(line.contains("$moduleId$")) {
					line = line.replace("$moduleId$", module.getModuleId());
				}
				
				if(line.contains("$type$")) {
					line = line.replace("$type$", module.getType());
				}
				
				if(line.contains("$title$")) {
					line = line.replace("$title$", module.getTitle());
				}
				
				if(line.contains("$iconCls$")) {
					line = line.replace("$iconCls$", module.getIconCls());
				}
				
				out.print(line);
			}
			reader.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void returnModul(HttpServletRequest request, HttpServletResponse response, String moduleId) {
		String[] pp = moduleId.split("-");
		String path = "/modules/" + pp[0] + "/" + pp[1] + "-win/client/" + pp[1] + "-win.js" ;
		String filePath = request.getSession().getServletContext().getRealPath(path);
		
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			PrintWriter out = response.getWriter();
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
			String line = null;
			while((line = reader.readLine()) != null) {
				out.print(line);
			}
			reader.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
